// set
// var detectCycle = function (head) {
//   const set = new Set()
//   while (head !== null) {
//     if (set.has(head)) return head
//     set.add(head)
//     head = head.next
//   }
//   return null
// };

// 快慢指针
var detectCycle = function (head) {
  let slow = head, fast = head
  if (head === null) return null
  while (fast !== null) {
    slow = slow.next
    if (fast.next) {
      fast = fast.next.next
    } else {
      return null
    }
    if (slow === fast) {
      let pre = head
      while (pre !== slow) {
        pre = pre.next
        slow = slow.next
      }
      return pre
    }
  }
  return null
};